/*
 * @描述: 数据权限数据请求
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import { querySysMenu, queryDataFieldByMenuCode, queryRoleCode, updateSysMenu } from '@/services/api';

export default {
  namespace: 'dataPermission',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    roleFieldData: {
      list: [],
      count: 0
    },
    roleSelectData: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysMenu, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchRoleFieldData({ payload }, { call, put }) {
      const response = yield call(queryDataFieldByMenuCode, payload);
      yield put({
        type: 'saveRoleMenu',
        payload: response
      });
    },
    *fetchRole({ payload }, { call, put }) {
      const response = yield call(queryRoleCode, payload);
      yield put({
        type: 'saveRole',
        payload: response
      });
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateSysMenu, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveRoleMenu(state, action) {
      return {
        ...state,
        roleFieldData: action.payload.data
      };
    },
    saveRole(state, action) {
      return {
        ...state,
        roleSelectData: action.payload.data
      };
    }
  }
};
